package com.yami.shop.rights.common.listener;

import com.alibaba.fastjson.JSONObject;
import com.yami.shop.bean.enums.RightsType;
import com.yami.shop.bean.event.KtVerificationRightsEvent;
import com.yami.shop.rights.common.strategy.PersonRightsFactory;
import com.yami.shop.rights.common.strategy.PersonRightsStrategy;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component("KtVerificationRightsListener")
@AllArgsConstructor
public class KtVerificationRightsListener {

    @Autowired
    private PersonRightsFactory personRightsFactory;

    @EventListener(KtVerificationRightsEvent.class)
    public void updateKtPersonRights(KtVerificationRightsEvent event) {
        log.info("==========空投权益完成修改======{}======{}",event.getPersonRightsId(),event.getStatus());
        if(event.getStatus() == 2){
            // 成功
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("type", RightsType.KTPROD_VOUCHER.value());
            jsonObject.put("personRightsKtId", event.getPersonRightsId());
            personRightsFactory.verificationRights(jsonObject);
        }else {
            // 失败
            personRightsFactory.cancelOrder(event.getPersonRightsId(),RightsType.KTPROD_VOUCHER.value());
        }
    }

}
